package com.malou.client.ventas.ingresostock;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class IngresoStockDialog extends Dialog {
	public HiddenField<String> modeloprenda_id;
	public TextField<String> modeloprenda;	
	private NumberField cantidad;
	private ComboBox<BeanModel> proveedor;
	private Button guardar;
	private Button cancelar;
	public Status status;

	IngresoStock ins;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public IngresoStockDialog(){
		setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(300, 170);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	        
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(cantidad);
	    
	    setIconStyle("icon-stock");
		setHeading(constants.stockIngresoStock());
	}
	
	@Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  IngresoStockDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
		    getButtonBar().disable();
		    
		    ins = new IngresoStock();
			
		    if(proveedor.getValue().get("id") != null)
		    ins.setProveedor_id(Integer.valueOf(proveedor.getValue().get("id").toString()));
		    ins.setFecha(new Date());
		    ins.setModeloprenda_id(Integer.valueOf(modeloprenda_id.getValue().toString()));
			
			if(Utilities.hasValue(cantidad))
  			ins.setCantidad(Integer.valueOf(cantidad.getValue().toString()));			
			
			IngresoStocks.ingresoStockDialog = IngresoStockDialog.this;   			
			IngresoStocks.save(ins);	    			
	    	}	    		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	}
	
	private boolean validate() {
		boolean can = cantidad.isValid();
		boolean prv = proveedor.isValid();		
	    return can && prv;
	}
	
	public void reset(){	  
		  		  
		  modeloprenda_id.clear();
		  modeloprenda.clear();
		  cantidad.clear();		  
		  
		  status.hide();
		  getButtonBar().enable();
	}
	
	private void addFields(ContentPanel cp) {

		modeloprenda_id = new HiddenField<String>();
		modeloprenda_id.setVisible(false);
		cp.add(modeloprenda_id);
		
		IngresoStocks.getComboProveedores();		
	    
	    proveedor = new ComboBox<BeanModel>();  
	    proveedor.setEditable(false);
	    proveedor.setFieldLabel(constants.stockProveedor());
	    proveedor.setDisplayField("razonsocial");
	    proveedor.setEmptyText("Seleccione...");
	    proveedor.setWidth(150);  
	    proveedor.setStore(IngresoStocks.storeProveedor);  
	    proveedor.setTypeAhead(true);  
	    proveedor.setTriggerAction(TriggerAction.ALL);
	    proveedor.setValueField("razonsocial");
	    proveedor.setAllowBlank(false);
	    proveedor.setUseQueryCache(false);
	    cp.add(proveedor, new FormData("-20"));
		
		modeloprenda = new TextField<String>();		
		modeloprenda.setFieldLabel(constants.stockModeloPrenda());
		modeloprenda.setEnabled(false);
	    cp.add(modeloprenda, new FormData("-20"));
	    	    
	    cantidad = new NumberField();
	    cantidad.setFieldLabel(constants.stockCantidad());
	    cantidad.setAllowNegative(false);
	    cantidad.setAllowBlank(false);
	    cantidad.setAllowDecimals(false);
	    cantidad.setPropertyEditorType(Integer.class);    
	    cp.add(cantidad, new FormData("-20"));		    

   }
}
